package com.qp.basics.admin.controller;


import com.qp.basics.admin.service.EntityHistoryService;
import com.qp.basics.admin.service.dto.EntityHistoryQueryCriteria;
import com.qp.basics.common.domain.EntityHistory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequiredArgsConstructor
@Api(tags = "实体修改历史管理")
@RequestMapping("/api/entityHistory")
public class EntityHistoryController {

    private final EntityHistoryService entityHistoryService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('entityHistory:list')")
    public void download(HttpServletResponse response, EntityHistoryQueryCriteria criteria) throws IOException {
        entityHistoryService.download(entityHistoryService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询实体修改历史")
    @PreAuthorize("@el.check('entityHistory:list')")
    public ResponseEntity<Object> query(EntityHistoryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(entityHistoryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增实体修改历史")
    @PreAuthorize("@el.check('entityHistory:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody EntityHistory resources){
        return new ResponseEntity<>(entityHistoryService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改实体修改历史")
    @PreAuthorize("@el.check('entityHistory:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EntityHistory resources){
        entityHistoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除实体修改历史")
    @PreAuthorize("@el.check('entityHistory:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        entityHistoryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导入")
    @PostMapping(value = "/import")
    public ResponseEntity<Object> importExcel(@RequestParam("file") MultipartFile file){
        return new ResponseEntity<Object>(entityHistoryService.importExcel(file),HttpStatus.OK);
    }
}
